unit home;

{$mode objfpc}{$H+}

interface

uses
  SysUtils,
  Classes,
  httpdefs,
  fpHTTP,
  fpWeb,
  templateutils;

type

  { TmodHome }

  TmodHome = class(TFPWebModule)
    procedure DataModuleAfterResponse(Sender: TObject; AResponse: TResponse);
    procedure DataModuleBeforeRequest(Sender: TObject; ARequest: TRequest);
    procedure DataModuleCreate(Sender: TObject);
    procedure indexAfterResponse(Sender: TObject; AResponse: TResponse);
    procedure indexBeforeRequest(Sender: TObject; ARequest: TRequest);
    procedure indexRequest(Sender: TObject; ARequest: TRequest;
      AResponse: TResponse; var Handled: boolean);
  private
    template: TMainTemplate;
    fHomeTemplate:THomeTemplate;
  public
    { public declarations }
  end;

var
  modHome: TmodHome;

implementation
{$R *.lfm}

{ TmodHome }

procedure TmodHome.DataModuleAfterResponse(Sender: TObject; AResponse: TResponse);
begin
  freeAndNil(template);
  freeAndNil(fHomeTemplate);
end;

procedure TmodHome.DataModuleBeforeRequest(Sender: TObject; ARequest: TRequest);
begin
  template := TMainTemplate.Create;
  fHomeTemplate:=THomeTemplate.create;
  template.ContentTemplate:=fHomeTemplate;
end;

procedure TmodHome.DataModuleCreate(Sender: TObject);
begin
  Actions.ActionByName('index').Default:=True;
  ActionVar:='action';
end;

procedure TmodHome.indexAfterResponse(Sender: TObject; AResponse: TResponse);
begin
  AResponse.Contents.Add(Template.getTemplate.GetContent);
  AResponse.SendContent;
  FreeAndNil(template);
end;

procedure TmodHome.indexBeforeRequest(Sender: TObject; ARequest: TRequest);
begin
  Template.PageTitle := 'HOME';
  Template.Title := 'Welcome to Hade - Framework!';
end;

procedure TmodHome.indexRequest(Sender: TObject; ARequest: TRequest;
  AResponse: TResponse; var Handled: boolean);
begin;
  Handled := True;
end;

initialization
  RegisterHTTPModule('Home', TModHome);
end.

